<?php 
class ControllerPhoneContact extends Controller {
	private $error = array(); 

	public function index() {
        $this->language->load('information/contact');

        $this->document->setTitle($this->language->get('heading_title'));

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $mail = new Mail();
            $mail->protocol = $this->config->get('config_mail_protocol');
            $mail->parameter = $this->config->get('config_mail_parameter');
            $mail->hostname = $this->config->get('config_smtp_host');
            $mail->username = $this->config->get('config_smtp_username');
            $mail->password = $this->config->get('config_smtp_password');
            $mail->port = $this->config->get('config_smtp_port');
            $mail->timeout = $this->config->get('config_smtp_timeout');
            $mail->setTo($this->config->get('config_email'));
            $mail->setFrom($this->request->post['email']);
            $mail->setSender($this->request->post['usuario']);
            $mail->setSubject(html_entity_decode(sprintf($this->request->post['subject'], $this->request->post['usuario']), ENT_QUOTES, 'UTF-8'));
            $mail->setText(strip_tags(html_entity_decode($this->request->post['mensaje'], ENT_QUOTES, 'UTF-8')));
            $mail->send();
            
			$this->data['sucess_message'] = $this->language->get('sucess_message');
        }

        if (isset($this->error['captcha'])) {
            $this->data['error_captcha'] = $this->error['captcha'];
        } else {
            $this->data['error_captcha'] = '';
        }

        $this->data['action'] = $this->url->link('phone/contact');

        if (isset($this->request->post['user'])) {
            $this->data['user'] = $this->request->post['user'];
        } else {
            $this->data['user'] = '';
        }

        if (isset($this->request->post['email'])) {
            $this->data['email'] = $this->request->post['email'];
        } else {
            $this->data['email'] = '';
        }

        if (isset($this->request->post['subject'])) {
            $this->data['subject'] = $this->request->post['subject'];
        } else {
            $this->data['subject'] = '';
        }

        if (isset($this->request->post['message'])) {
            $this->data['message'] = $this->request->post['message'];
        } else {
            $this->data['message'] = '';
        }

        if (isset($this->request->post['captcha'])) {
            $this->data['captcha'] = $this->request->post['captcha'];
        } else {
            $this->data['captcha'] = '';
        }

        $this->document->setTitle($this->config->get('config_title'));
        $this->document->setDescription($this->config->get('config_meta_description'));
        $this->data['heading_title'] = $this->config->get('config_title');
        $this->template = 'ukphone/template/phone/contact.tpl';
        
        $this->data['title'] = $this->language->get('heading_title');
        $this->data['text_message'] = sprintf($this->language->get('text_message'), $this->url->link('phone/number/report'), '/legal');
        $this->data['text_your_name'] = $this->language->get('text_your_name');
        $this->data['text_your_email'] = $this->language->get('text_your_email');
        $this->data['text_subject'] = $this->language->get('text_subject');
        $this->data['text_comment'] = $this->language->get('text_comment');
        
        $this->data['text_note_your_name'] = $this->language->get('text_note_your_name');
        $this->data['text_note_your_email'] = $this->language->get('text_note_your_email');
        $this->data['text_note_subject'] = $this->language->get('text_note_subject');
        $this->data['text_note_comment'] = $this->language->get('text_note_comment');
        
        $this->load->model('phone/call_type');

        $this->data['call_types'] = $this->model_phone_call_type->getCallTypes();

        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render());
	}

	protected function validate() {
		if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
			$this->error['captcha'] = $this->language->get('error_captcha');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}  	  
	}

	public function captcha() {
		$this->load->library('captcha');

		$captcha = new Captcha();

		$this->session->data['captcha'] = $captcha->getCode();

		$captcha->showImage();
	}
}
?>
